package com.fireline.beauty.member;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.fireline.beauty.ContextHelp;
import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.controls.DataCombo;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.frame.FunctionDialog;
import com.zgw.fireline.base.frame.IFunction;
import com.zgw.fireline.base.widgets.Batch;
import com.zgw.fireline.base.widgets.BatchItem;
import com.zgw.fireline.base.widgets.ControlUtil;

/**
 * 会员充值窗口
 * */
public class HyjfEdit implements IFunction {

	public int result;
	private Shell shell;
	private Text textYfje;
	private Text textYhje;
	private Text textSjje;
	private Text textBz;
	/**
	 * @wbp.nonvisual location=17,382
	 */
	private final Dataset hyxxSet = new Dataset(
			"select * from hyxx where \r\n hybh =?", SqliteJdbcImpl.INSTANCE);
	private CLabel lblNewLabel_1;
	private String hybh;
	private CLabel label_1;
	private CLabel label_3;
	private CLabel label_5;
	/**
	 * @wbp.nonvisual location=87,382
	 */
	private final Dataset ygSet = new Dataset("select * from ygxx",
			SqliteJdbcImpl.INSTANCE);

	public HyjfEdit(String hybh) {
		this.hybh = hybh;
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	public Control createControl(Composite parent) {
		this.shell = parent.getShell();
		hyxxSet.setKeyColumns(new String[] {});
		ygSet.setKeyColumns(new String[] {});
		Composite content = new Composite(parent, SWT.NONE);
		GridLayout gl_content = new GridLayout(1, false);
		gl_content.marginWidth = 10;
		content.setLayout(gl_content);

		Group group = new Group(content, SWT.NONE);
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));
		group.setText("会员资料");
		GridLayout gl_group = new GridLayout(4, false);
		gl_group.verticalSpacing = 10;
		gl_group.marginHeight = 10;
		gl_group.marginWidth = 20;
		gl_group.horizontalSpacing = 0;
		group.setLayout(gl_group);

		Label lblNewLabel = new Label(group, SWT.NONE);
		lblNewLabel.setText("会员编号：");

		lblNewLabel_1 = new CLabel(group, SWT.NONE);
		lblNewLabel_1.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		lblNewLabel_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		lblNewLabel_1
				.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));

		Label label = new Label(group, SWT.NONE);
		GridData gd_label = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1,
				1);
		gd_label.horizontalIndent = 50;
		label.setLayoutData(gd_label);
		label.setText("会员编号：");

		label_1 = new CLabel(group, SWT.NONE);
		label_1.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		label_1.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false,
				1, 1));
		label_1.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));

		Label label_2 = new Label(group, SWT.NONE);
		label_2.setText("联系电话：");

		label_3 = new CLabel(group, SWT.NONE);
		label_3.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		label_3.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false,
				1, 1));
		label_3.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));

		Label label_4 = new Label(group, SWT.NONE);
		label_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_4.setText("卡内金额：");

		label_5 = new CLabel(group, SWT.NONE);
		label_5.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		label_5.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false,
				1, 1));
		label_5.setForeground(SWTResourceManager.getColor(SWT.COLOR_BLUE));

		Group group_1 = new Group(content, SWT.NONE);
		group_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		group_1.setText("储值卡续费");
		GridLayout gl_group_1 = new GridLayout(4, false);
		gl_group_1.horizontalSpacing = 0;
		gl_group_1.verticalSpacing = 10;
		gl_group_1.marginWidth = 20;
		group_1.setLayout(gl_group_1);

		Label label_6 = new Label(group_1, SWT.NONE);
		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_6.setText("充值金额：");

		textYfje = new Text(group_1, SWT.BORDER);
		textYfje.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		textYfje.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		textYfje.setText("0");
		ControlUtil.setCurrencyRule(textYfje, false, false);
		textYfje.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				doInput();
			}
		});
		GridData gd_textYfje = new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1);
		gd_textYfje.widthHint = 125;
		textYfje.setLayoutData(gd_textYfje);

		Label label_7 = new Label(group_1, SWT.NONE);
		GridData gd_label_7 = new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1);
		gd_label_7.horizontalIndent = 20;
		label_7.setLayoutData(gd_label_7);
		label_7.setText("赠送金额：");

		textYhje = new Text(group_1, SWT.BORDER);
		textYhje.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		textYhje.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		ControlUtil.setCurrencyRule(textYhje, false, false);
		textYhje.setText("0");
		textYhje.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				doInput();
			}
		});

		GridData gd_textYhje = new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1);
		gd_textYhje.widthHint = 125;
		textYhje.setLayoutData(gd_textYhje);

		Label label_8 = new Label(group_1, SWT.NONE);
		label_8.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_8.setText("实际存入：");

		textSjje = new Text(group_1, SWT.BORDER | SWT.READ_ONLY);
		textSjje.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
		textSjje.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		textSjje.setText("0");
		textSjje.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		ControlUtil.setCurrencyRule(textSjje, false, false);
		Label label_9 = new Label(group_1, SWT.NONE);
		label_9.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_9.setText("经办员工：");

		DataCombo comboYgbh = new DataCombo(group_1, SWT.READ_ONLY, ygSet);
		comboYgbh.setFont(SWTResourceManager.getFont("宋体", 9, SWT.NORMAL));
		comboYgbh.setShowText("${ygmc}");
		comboYgbh.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));

		Label label_10 = new Label(group_1, SWT.NONE);
		label_10.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		label_10.setText("备注信息：");

		textBz = new Text(group_1, SWT.BORDER);
		textBz.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3,
				1));
		group_1.setTabList(new Control[] { textYfje, textYhje, comboYgbh,
				textBz });

		Composite composite = new Composite(content, SWT.NONE);
		RowLayout rl_composite = new RowLayout(SWT.HORIZONTAL);
		rl_composite.marginRight = 0;
		rl_composite.spacing = 10;
		composite.setLayout(rl_composite);
		composite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));

		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		btnNewButton_1.setLayoutData(new RowData(80, SWT.DEFAULT));
		btnNewButton_1.setText("保存&S");
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSave();
			}
		});

		Button btnNewButton = new Button(composite, SWT.NONE);

		btnNewButton.setLayoutData(new RowData(80, SWT.DEFAULT));
		btnNewButton.setText("退出&E");
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				result = SWT.CANCEL;
				shell.close();
			}
		});

		Listener focusInListener = new Listener() {
			@Override
			public void handleEvent(Event event) {
				((Text) event.widget).selectAll();
			}
		};

		for (Control c : group_1.getTabList()) {
			if (c instanceof Text) {
				c.addListener(SWT.FocusIn, focusInListener);
			}
			ControlUtil.setEnterToFocusMove(c);
		}
		setParams();
		createGUIAfter();
		setValues();
		return content;
	}

	protected void doValdate() {

	}

	DecimalFormat format = new DecimalFormat("0.00");

	protected void doInput() {
		BigDecimal sjje = new BigDecimal(textYfje.getText().trim())
				.add(new BigDecimal(textYhje.getText().trim()));
		textSjje.setText(format.format(sjje));
	}

	/**
	 * 保存信息
	 * */
	protected void doSave() {
		FkDialog dlg = new FkDialog(shell, SWT.NONE, 0);
		dlg.yfje = textYfje.getText();
		if (dlg.open() == SWT.OK) {
			String lb = "0";
			String lbsm = "储值卡交费";
			String yfje = textYfje.getText();
			String yhje = textYhje.getText();
			String sjje = textSjje.getText();
			String ygbh = ygSet.getString("ygbh");
			String dqje = format.format(new BigDecimal(hyxxSet
					.getString("knje")).add(new BigDecimal(sjje)));
			Batch batch = new Batch(SqliteJdbcImpl.INSTANCE);
			BatchItem batchItem = new BatchItem(batch);
			batchItem
					.setCommand(" insert into hyjf(id,hybh,rq,czy,yfje,jfje,yhje,sjje,yh,fkyh,qk,fkqk,zt,ztsm,tcbh,dqje,xj,xyk,djq,czk,bz,ygbh,fd)\r\n       values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
			batchItem.setName("添加交费记录");
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
			SimpleDateFormat format2 = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			String id = format.format(new Date());
			batch.setParam(0, 0, id);// id
			batch.setParam(0, 1, hybh);// hybh
			batch.setParam(0, 2, format2.format(new Date()));// rq
			batch.setParam(0, 3, ContextHelp.getUserName());// czy
			batch.setParam(0, 4, yfje);// yfje 应付金额
			batch.setParam(0, 5, dlg.jfje);// jfje 交费金额
			batch.setParam(0, 6, yhje);// yhje 优惠赠送金额
			batch.setParam(0, 7, sjje);// sjje 实际入帐额
			batch.setParam(0, 8, dlg.fkyh); // yh 优惠
			// fkyh 付款优惠
			batch.setParam(0, 9, dlg.fkyh);
			batch.setParam(0, 10,  dlg.fkqk); // qk 欠款
			// fkqk 付款欠款
			batch.setParam(0, 11, dlg.fkqk);
			batch.setParam(0, 12, lb); // zt 状态即 付款类别
			batch.setParam(0, 13, lbsm);// ztsm 状态说明
			batch.setParam(0, 14, "");// tcbh套餐编号
			batch.setParam(0, 15, dqje);// dqje卡内当前金额
			batch.setParam(0, 16, dlg.fkfs.equals("xj") ? dlg.jfje : "0");// xj
																			// 现金
			batch.setParam(0, 17, dlg.fkfs.equals("xyk") ? dlg.jfje : "0");// xyk
																			// 信用卡
			batch.setParam(0, 18, dlg.fkfs.equals("djq") ? dlg.jfje : "0");// djq
																			// 代金券
			batch.setParam(0, 19, "0");// czk 储值卡
			batch.setParam(0, 20, textBz.getText());// bz 备注
			batch.setParam(0, 21, ygbh);// ygbh 员工编号
			batch.setParam(0, 22, "1");// fd

			BatchItem batchItem_1 = new BatchItem(batch);
			batchItem_1.setCommand("update hyxx set knje=?\r\nwhere hybh=?");
			batchItem_1.setName("修改会员卡佘额");
			batch.setParam(1, 0, dqje);
			batch.setParam(1, 1, hybh);

			// 添加佘额变动记录
			BatchItem y = new BatchItem(batch);
			y.setName("添加佘额变动记录");
			y.setCommand("INSERT INTO yebd( hybh, lx, rq, je_inc, je_dec, cs_inc, cs_dec, sycs, syje, czy, bz, xmbh, fd, xfid)"
								+" VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
			y.setParam(0, hybh);
			y.setParam(1, "储值卡交费");
			y.setParam(2, format2.format(new Date()));
			y.setParam(3, sjje);
			y.setParam(4, "0");
			y.setParam(5, "0");
			y.setParam(6, "0");
			y.setParam(7, "0");
			y.setParam(8, dqje);
			y.setParam(9, ContextHelp.getUserName());
			y.setParam(10, "充值单号:"+id);
			y.setParam(11, "");
			y.setParam(12, "");
			y.setParam(13, id);
			try {
				batch.execute();
				result = SWT.OK;
				shell.close();
			} catch (Exception e) {
				e.printStackTrace();
				MessageDialog
						.openError(shell, "程序异常", "交费失败:" + e.getMessage());
			}

		}
	}

	protected void setParams() {
		hyxxSet.setParam(0, hybh);
	}

	protected void setValues() {
		lblNewLabel_1.setText(hyxxSet.getValue("hybh", String.class, ""));
		label_1.setText(hyxxSet.getValue("hyxm", String.class, ""));
		label_3.setText(hyxxSet.getValue("lxdh", String.class, ""));
		label_5.setText(hyxxSet.getStringForExpr("${knje}元"));
	}

	protected void createGUIAfter() {
		hyxxSet.update();
		ygSet.update();
		if (hyxxSet.getCount() > 0)
			hyxxSet.setSelected(0);
	}

	public static void main(String[] args) {
		HyjfEdit hyjf = new HyjfEdit("000002");
		FunctionDialog dialog = new FunctionDialog(new Shell(), hyjf);
		dialog.open();
	}
}
